package cn.lyj.study.util;

import java.util.List;

/**
 * 自定义分页工具
 * @author lyj
 * @data 2021-08-16
 */
public class PageInfo<T> {
    //  数据库一共有多少条记录
    private int totalRows ;
    //  一共能分出多少页
    private int totalPages ;
    //每页数据的个数
    private int pageSize ;
    //  当前页
    private int currentPage = 1 ;
    // 每页开始的位置
    private int start ;
    //每页所需的数据
    private List<T> list ;
    // 每页结束的位置
    private int end ;

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public PageInfo(int totalRows, int pageSize) {
        this.totalRows = totalRows;
        this.pageSize = pageSize;

        // 一共分出多少页
        if(this.totalRows%this.pageSize == 0){
            this.totalPages = this.totalRows/this.pageSize ;
        }else {
            this.totalPages = this.totalRows/this.pageSize + 1 ;
        }
    }
    //  分页逻辑
    public void doPaging(int reqCurrentPage){
        this.currentPage = reqCurrentPage ;
        this.start = (this.currentPage-1)*this.pageSize ;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "totalRows=" + totalRows +
                ", totalPages=" + totalPages +
                ", pageSize=" + pageSize +
                ", currentPage=" + currentPage +
                ", start=" + start +
                ", list=" + list +
                ", end=" + end +
                '}';
    }
}
